package com.jyserver.common.AutoCode.jybaits;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;

import java.util.ArrayList;
import java.util.List;

public class MyJavaFormatter {

    private String filename;
    private String table;
    /**
     * @Description :普通请求参数注解
     */
    private String swaggerString;

    /**
     * @Description :分页普通请求参数注解
     */
    private String swaggerStringPage;

    private String tablecomment;
    private String RequiresPermissions_str = "";

    private String RequiresRoles_str = "";
    private String keytype = "String";


    private boolean RequiresRoles = false;

    private boolean RequiresPermissions = false;

    public boolean isRequiresPermissions() {
        return RequiresPermissions;
    }

    public void setRequiresPermissions(boolean requiresPermissions) {
        RequiresPermissions = requiresPermissions;
    }

    private List<String> functions = new ArrayList<String>();

    private List<String> imports = new ArrayList<String>();

    private String GeneratorController_targetPackage;

    private List<String> hadecomment = new ArrayList<String>();

    private List<String> Mappers = new ArrayList<String>();

    private IntrospectedTable introspectedTable;

    private List<IntrospectedColumn> introspectedColumns;
    private String Controllerhead;

    private String tabletype;
    private String ExampleType;
    private String keyname = "keyname";


    public MyJavaFormatter(IntrospectedTable arg1, String GeneratorController_targetPackage, String controllerhead) {
        this.introspectedTable = arg1;
        this.introspectedColumns = introspectedTable.getAllColumns();
        this.Controllerhead = controllerhead;
        MyJavaFormatter(arg1.getFullyQualifiedTable().getIntrospectedTableName(), arg1.getChinese(), arg1.getMyBatis3JavaMapperType(), GeneratorController_targetPackage);
    }

    public void MyJavaFormatter(String table, String tablecomment, String Mapper, String GeneratorController_targetPackage) {
        setTable(table);
        this.tablecomment = tablecomment;
        setTablecomment(tablecomment);
        if (introspectedTable.getPrimaryKeyColumns().size() > 0) {
            keyname = introspectedTable.getPrimaryKeyColumns().get(0).getActualColumnName();
        }

        this.filename = Mapper.substring(Mapper.lastIndexOf(".") + 1).replace("Mapper", "") + "Controller.java";
        this.GeneratorController_targetPackage = GeneratorController_targetPackage;
        this.imports.add("io.swagger.annotations.ApiModel");
        this.imports.add("io.swagger.annotations.ApiOperation");
        this.imports.add("io.swagger.annotations.ApiImplicitParams");
        this.imports.add("io.swagger.annotations.ApiImplicitParam");
        this.imports.add("io.swagger.annotations.Api");


        this.imports.add("org.springframework.web.bind.annotation.RequestMapping");
        this.imports.add("org.springframework.stereotype.Controller");
        this.imports.add("org.springframework.web.bind.annotation.ResponseBody");
        this.imports.add("org.springframework.beans.factory.annotation.Autowired");

        this.imports.add("org.springframework.boot.autoconfigure.EnableAutoConfiguration");
        this.imports.add("org.springframework.context.annotation.ComponentScan");
        this.imports.add("org.springframework.web.bind.annotation.RequestParam");

        this.imports.add("javax.servlet.http.HttpServletRequest");
        this.imports.add("javax.servlet.http.HttpServletResponse");
        this.imports.add("java.util.List");
        this.imports.add("org.apache.log4j.Logger");

        this.imports.add("com.alibaba.fastjson.JSON");
        this.imports.add("com.alibaba.fastjson.JSONObject");
        this.imports.add("com.alibaba.fastjson.JSONArray");


//        this.imports.add("com.shopping.shopping.eastmoney.analysisUtils");
//        this.imports.add("com.shopping.shopping.eastmoney.api.AllUrl");
//        this.imports.add("com.shopping.shopping.eastmoney.api.ApiUtils");
//        this.imports.add("com.shopping.shopping.eastmoney.api.BkAllApi");
//        this.imports.add("com.shopping.shopping.eastmoney.analysis");
        this.imports.add("com.jyserver.common.utils.JyHttp");
        this.imports.add("com.jyserver.common.utils.HelpUtils");
        this.imports.add("com.jyserver.common.utils.Utils");

        this.imports.add("com.jyserver.common.model.ResultList");
        this.imports.add("com.jyserver.common.model.ResultMsg");
        this.imports.add("org.springframework.web.bind.annotation.RequestMethod");


        this.imports.add("org.junit.Test");


        this.imports.add(introspectedTable.getBaseRecordType());
        this.imports.add(introspectedTable.getExampleType());
        this.Mappers.add(Mapper);
        this.tabletype = introspectedTable.getBaseRecordType().substring(introspectedTable.getBaseRecordType().lastIndexOf(".") + 1);
        this.ExampleType = introspectedTable.getExampleType().substring(introspectedTable.getExampleType().lastIndexOf(".") + 1);
       System.err.println(introspectedTable.getExampleType());
        swaggerString = "\t@ApiImplicitParams({\n";
        for (int i = 0; i < introspectedColumns.size(); i++) {
            String str = introspectedColumns.get(i).getFullyQualifiedJavaType().toString();
            String[] types = str.replace(".", ",").split(",");
            String s = types[types.length - 1];
            swaggerString = swaggerString + "\t\t\t@ApiImplicitParam(paramType=\"query\", name = \"" + introspectedColumns.get(i).getJavaProperty() + "\", value = \"" + introspectedColumns.get(i).getChinese() + "\", required =true, dataType = \"" + s + "\"),\n";
        }
        swaggerStringPage = swaggerString + "\t\t\t@ApiImplicitParam(paramType=\"query\", name = \"pagesIndex\", value = \"请求页面数\", required =true, dataType = \"Int\"),\n";
        swaggerStringPage = swaggerStringPage + "\t\t\t@ApiImplicitParam(paramType=\"query\", name = \"pagesSize\", value = \"页面记录数量\", required =true, dataType = \"Int\"),\n";
        swaggerString = swaggerString + "\t})";
        swaggerStringPage = swaggerStringPage + "\t})";

    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public String getTablecomment() {
        return tablecomment;
    }

    public void setTablecomment(String tablecomment) {
        this.tablecomment = tablecomment;
    }

    public List<String> getFunctions() {
        return functions;
    }

    public void setFunctions(List<String> functions) {
        this.functions = functions;
    }

    public List<String> getImports() {
        return imports;
    }

    public void setImports(List<String> imports) {
        this.imports = imports;
    }

    public String getSwaggerString() {
        return swaggerString;
    }

    public void setSwaggerString(String swaggerString) {
        this.swaggerString = swaggerString;
    }

    public boolean isRequiresRoles() {
        return RequiresRoles;
    }

    public void setRequiresRoles(boolean requiresRoles) {
        RequiresRoles = requiresRoles;
    }

    public String getGeneratorController_targetPackage() {
        return GeneratorController_targetPackage;
    }

    public void setGeneratorController_targetPackage(String generatorController_targetPackage) {
        GeneratorController_targetPackage = generatorController_targetPackage;
    }

    public List<String> getHadecomment() {
        return hadecomment;
    }

    public void setHadecomment(List<String> hadecomment) {
        this.hadecomment = hadecomment;
    }

    public void setMappers(List<String> mappers) {
        Mappers = mappers;
    }

    public IntrospectedTable getIntrospectedTable() {
        return introspectedTable;
    }

    public void setIntrospectedTable(IntrospectedTable introspectedTable) {
        this.introspectedTable = introspectedTable;
    }

    public String getFilename() {
        return this.filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public void addfunctions(String str) {
        this.functions.add(str);
    }


    public String getfunctions() {
        String str = "\n\n";
        for (int i = 0; i < this.functions.size(); i++) {
            str = str + this.functions.get(i) + "\n\n";
        }
        return str;
    }

    public void addMappers(String str) {
        this.Mappers.add(str);
    }


    public String getMappers() {
        String str = "\n\n";
        for (int i = 0; i < this.Mappers.size(); i++) {
            str = str + "\t@Autowired\n";
            str = str + "\tprivate  " + this.Mappers.get(i).substring(this.Mappers.get(i).lastIndexOf(".") + 1) + "\t" + this.Mappers.get(i).substring(this.Mappers.get(i).lastIndexOf(".") + 1).toLowerCase() + ";\n\n";
        }
        return str;
    }

    public void addimports(String str) {
        this.imports.add(str);
    }

    public String getimports() {
        String str = "\n\n";
        for (int i = 0; i < this.imports.size(); i++) {
            str = str + "import " + this.imports.get(i) + ";\n";
        }
        for (int i = 0; i < this.Mappers.size(); i++) {
            str = str + "import " + this.Mappers.get(i) + ";\n";
        }
        return str;
    }

    public void addhadecomment(String str) {
        this.hadecomment.add(str);
    }

    public String gethadecomment() {
        String str = "";
        for (int i = 0; i < this.hadecomment.size(); i++) {
            str = str + this.hadecomment.get(i) + "\n";
        }
        return str;
    }

    public String getallstr() {
        selectByExample();
        insertSelective();
        insert();
        System.err.println(introspectedTable.getGeneratedKey());
        System.err.println(introspectedTable.getPrimaryKeyColumns());
        if (introspectedTable.getPrimaryKeyColumns().size() > 0) {
            String str = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().toString();
            String[] types = str.replace(".", ",").split(",");
            keytype = types[types.length - 1];
            updateByPrimaryKey();
            updateByPrimaryKeySelective();
            selectByPrimaryKey();
            deleteByPrimaryKey();
        }
//        getDataEastmoneyAnalysisUtils();
//        getDataEastmoneyAnalysis();
//        getDataEastmoneyAnalysisUtilsTest();
//        getDataEastmoneyAnalysisTest();
        String str = "package  " + this.GeneratorController_targetPackage + ";\n";
        str = str + gethadecomment() + getimports();
        str = str + "@Controller\n@ComponentScan\n" +
                "@EnableAutoConfiguration\n@RequestMapping(\"/" + Controllerhead + "/" + this.filename.replace(".java", "") + "/\")\n" + "@Api(value = \"" + this.tablecomment + "\", tags= \"" + this.tablecomment + "\",description=\"" + this.tablecomment + "\")\n";
        str = str + "public class " + this.filename.replace(".java", "") + " {\n";
        str = str + "\tprivate \tLogger \tlogger = Logger.getLogger(" + this.filename.replace(".java", "") + ".class);";
        str = str + getMappers() + getfunctions();
        str = str + "\n}";
        return str;
    }


    public void selectByExample() {
        String str = "\t@RequestMapping(value=\"search\",method = {RequestMethod.POST})\n";
        if (RequiresRoles) {
            str = str + "\t@RequiresRoles(value={\"" + table + ":list\"}, logical= Logical.OR)";
            RequiresRoles_str = RequiresRoles_str + "";
        }
        if (RequiresPermissions) {
            str = str + "\t@RequiresPermissions(value={\"" + table + ":list\"}, logical= Logical.OR)";
        }
        str = str + "\t@ApiOperation(value=\"查询" + introspectedTable.getChinese() + "\", notes=\"\")\n" + swaggerStringPage + "\n";
        str = str + "\t@ResponseBody\n";
        str = str + "\tpublic  ResultList<" + tabletype + "> search(HttpServletRequest request) throws Exception {\n";
        str = str + "\t\t" + tabletype + "  " + tabletype.toLowerCase() + "= JyHttp.getRequestEty(request, " + tabletype + ".class);\n";
        str = str + "\t\t" + ExampleType + "  " + ExampleType.toLowerCase() + " = new " + ExampleType + "();\n";
        str = str + "\t\tLong count=" + this.Mappers.get(0).substring(this.Mappers.get(0).lastIndexOf(".") + 1).toLowerCase() + ".countByExample(" + ExampleType.toLowerCase() + ");\n";
        str = str + "\t\tList<" + tabletype + "> lists=" + this.Mappers.get(0).substring(this.Mappers.get(0).lastIndexOf(".") + 1).toLowerCase() + ".selectByExample(" + ExampleType.toLowerCase() + ");\n";
        str = str + "\t\treturn new ResultList(lists);\n";
        str = str + "\t}";
        functions.add(str);
    }


    public void insertSelective() {
        String str = "\t@RequestMapping(value=\"insertSelective\",method = {RequestMethod.POST})\n";
        if (RequiresRoles) {
            str = str + "\t@RequiresRoles(value={\"" + table + ":save\"}, logical= Logical.OR)";
        }
        if (RequiresPermissions) {
            str = str + "\t@RequiresPermissions(value={\"" + table + ":save\"}, logical= Logical.OR)";
        }
        str = str + "\t@ApiOperation(value=\"保存" + introspectedTable.getChinese() + "（动态）\", notes=\"动态插入\")\n" + swaggerString + "\n";
        str = str + "\t@ResponseBody\n";
        str = str + "\tpublic  ResultMsg insertSelective(HttpServletRequest request) throws Exception {\n";
        str = str + "\t\ttry{\n";
        str = str + "\t\t\t" + tabletype + "  " + tabletype.toLowerCase() + "= JyHttp.getRequestEty(request, " + tabletype + ".class);\n";
//        str = str + "\t\t\t" + ExampleType + "  " + ExampleType.toLowerCase() + " = new " + ExampleType + "();\n";
        str = str + "\t\t\t" + this.Mappers.get(0).substring(this.Mappers.get(0).lastIndexOf(".") + 1).toLowerCase() + ".insertSelective(" + tabletype.toLowerCase() + ");\n";
        str = str + "\t\t\treturn new ResultMsg(\"添加成功\",true);\n";
        str = str + "\t\t}catch (Exception e){\n\t\t\tlogger.error(e.getMessage());\n\t\t}\n\t\treturn new ResultMsg(\"添加失败\",false);\n\t}";
        functions.add(str);
    }

    public void getDataEastmoneyAnalysisUtils() {
        String str = "";
        str = str + "\t@ApiOperation(value=\"从东方财富获取" + introspectedTable.getChinese() + "\", notes=\"动态插入\")\n" + swaggerString + "\n";
        str = str + "\t@ResponseBody\n";
        str = str + "\tpublic  Object getDataEastmoneyAnalysisUtils(HttpServletRequest request) throws Exception {\n";
        str = str + "\t\tApiUtils api = new ApiUtils(AllUrl.BkbaseApi);\n" +
                "\t\tList<String[]> lists = (List<String[]>) analysisUtils.jQuery(api, api.getString()).getDatas();\n" +
                "\t\tfor (int i = 1; i < api.getPages(); i++) {\n" +
                "\t\t\tapi.setP(i);\n" +
                "\t\t\tlists = (List<String[]>) analysisUtils.jQuery(api, api.getString()).getDatas();\n" +
                "\t\t\tfor (int k = 0; k < lists.size(); k++) {\n" +
                "\t\t\t\t" + tabletype + "  " + tabletype.toLowerCase() + "= JyHttp.getRequestEty(request, " + tabletype + ".class);\n" +
                "\t\t\t\t" + this.Mappers.get(0).substring(this.Mappers.get(0).lastIndexOf(".") + 1).toLowerCase() + ".insertSelective(" + tabletype.toLowerCase() + ");\n" +
                "\t\t\t }\n" +
                "\t\t}\n\t\treturn null;\n";
        str = str + "\t}";
        functions.add(str);
    }


    public void getDataEastmoneyAnalysisUtilsTest() {
        String str = "";
        str = str + "\t@ApiOperation(value=\"从东方财富获取" + introspectedTable.getChinese() + "\", notes=\"动态插入\")\n" + swaggerString + "\n";
        str = str + "\t@Test\n\tpublic void  getDataEastmoneyAnalysisUtilsTest() throws Exception {\n";
        str = str + "\t\tApiUtils api = new ApiUtils(AllUrl.BkbaseApi);\n" +
                "\t\tList<String[]> lists = (List<String[]>) analysisUtils.jQuery(api, api.getString()).getDatas();\n" +
                "\t\tfor (int i = 1; i < api.getPages(); i++) {\n" +
                "\t\t\tapi.setP(i);\n" +
                "\t\t\tlists = (List<String[]>) analysisUtils.jQuery(api, api.getString()).getDatas();\n" +
                "\t\t\tfor (int k = 0; k < lists.size(); k++) {\n" +
                "\t\t\t\t" + tabletype + "  " + tabletype.toLowerCase() + "= new " + tabletype + "();\n" +
                "\t\t\t }\n" +
                "\t\t}\n";
        str = str + "\t}";
        functions.add(str);
    }


    public void getDataEastmoneyAnalysis() {
        String str = "";
        str = str + "\t@ApiOperation(value=\"从东方财富获取" + introspectedTable.getChinese() + "\", notes=\"动态插入\")\n" + swaggerString + "\n";
        str = str + "\t@ResponseBody\n";
        str = str + "\tpublic  Object getDataEastmoneyAnalysis(HttpServletRequest request) throws Exception {\n";
        str = str + "\t\tBkAllApi api = new BkAllApi();\n" +
                "\t\tList<String[]> lists = (List<String[]>) analysis.jQuery(api, api.getString()).getDatas();\n" +
                "\t\tfor (int i = 1; i < api.getPages(); i++) {\n" +
                "\t\t\tapi.setP(i);\n" +
                "\t\t\tlists = (List<String[]>) analysis.jQuery(api, api.getString()).getDatas();\n" +
                "\t\t\tfor (int k = 0; k < lists.size(); k++) {\n" +
                "\t\t\t\t" + tabletype + "  " + tabletype.toLowerCase() + "= JyHttp.getRequestEty(request, " + tabletype + ".class);\n" +
                "\t\t\t\t" + this.Mappers.get(0).substring(this.Mappers.get(0).lastIndexOf(".") + 1).toLowerCase() + ".insertSelective(" + tabletype.toLowerCase() + ");\n" +
                "\t\t\t }\n" +
                "\t\t}\n\t\treturn null;\n";
        str = str + "\t}";
        functions.add(str);
    }


    public void getDataEastmoneyAnalysisTest() {
        String str = "";
        str = str + "\t@ApiOperation(value=\"从东方财富获取" + introspectedTable.getChinese() + "\", notes=\"測試\")\n" + swaggerString + "\n";
        str = str + "\t@Test\n\tpublic void getDataEastmoneyAnalysisTest() throws Exception {\n";
        str = str + "\t\tBkAllApi api = new BkAllApi();\n" +
                "\t\tList<String[]> lists = (List<String[]>) analysis.jQuery(api, api.getString()).getDatas();\n" +
                "\t\tfor (int i = 1; i < api.getPages(); i++) {\n" +
                "\t\t\tapi.setP(i);\n" +
                "\t\t\tlists = (List<String[]>) analysis.jQuery(api, api.getString()).getDatas();\n" +
                "\t\t\tfor (int k = 0; k < lists.size(); k++) {\n" +
                "\t\t\t\t" + tabletype + "  " + tabletype.toLowerCase() + "= new " + tabletype + "();\n" +
                "\t\t\t }\n" +
                "\t\t}\n";
        str = str + "\t}";
        functions.add(str);
    }

    public void insert() {
        String str = "\t@RequestMapping(value=\"insert\",method = {RequestMethod.POST})\n";
        if (RequiresRoles) {
            str = str + "\t@RequiresRoles(value={\"" + table + ":allsave\"}, logical= Logical.OR)";
        }
        if (RequiresPermissions) {
            str = str + "\t@RequiresPermissions(value={\"" + table + ":allsave\"}, logical= Logical.OR)";
        }
        str = str + "\t@ApiOperation(value=\"保存" + introspectedTable.getChinese() + "（完全保存）\", notes=\"完全插入\")\n" + swaggerString + "\n";
        str = str + "\t@ResponseBody \n";
        str = str + "\tpublic ResultMsg insert(HttpServletRequest request) throws Exception {\n";
        str = str + "\t\ttry{\n";
        str = str + "\t\t\t" + tabletype + "  " + tabletype.toLowerCase() + "= JyHttp.getRequestEty(request, " + tabletype + ".class);\n";
//        str = str + "\t\t\t" + ExampleType + "  " + ExampleType.toLowerCase() + " = new " + ExampleType + "();\n";
        str = str + "\t\t\t" + this.Mappers.get(0).substring(this.Mappers.get(0).lastIndexOf(".") + 1).toLowerCase() + ".insert(" + tabletype.toLowerCase() + ");\n";
        str = str + "\t\t\treturn new ResultMsg(\"添加成功\",true);\n";
        str = str + "\t\t}catch (Exception e){\n\t\t\tlogger.error(e.getMessage());\n\t\t}\n\t\treturn new ResultMsg(\"添加失败\",false);\n\t}";
        functions.add(str);
    }

    public void updateByPrimaryKeySelective() {
        String str = "\t@RequestMapping(value=\"update\",method = {RequestMethod.POST})\n";
        if (RequiresRoles) {
            str = str + "\t@RequiresRoles(value={\"" + table + ":update\"}, logical= Logical.OR)";
        }
        if (RequiresPermissions) {
            str = str + "\t@RequiresPermissions(value={\"" + table + ":update\"}, logical= Logical.OR)";
        }
        str = str + "\t@ApiOperation(value=\"根据条件修改" + introspectedTable.getChinese() + "\", notes=\"动态修改\")\n" + swaggerString + "\n";
        str = str + "\t@ResponseBody\n";
        str = str + "\tpublic  ResultMsg updateByPrimaryKeySelective(HttpServletRequest request) throws Exception {\n";
        str = str + "\t\ttry{\n";
        str = str + "\t\t\t" + tabletype + "  " + tabletype.toLowerCase() + "= JyHttp.getRequestEty(request, " + tabletype + ".class);\n";
//        str = str + "\t\t\t" + ExampleType + "  " + ExampleType.toLowerCase() + " = new " + ExampleType + "();\n";
        str = str + "\t\t\t" + this.Mappers.get(0).substring(this.Mappers.get(0).lastIndexOf(".") + 1).toLowerCase() + ".updateByPrimaryKeySelective(" + tabletype.toLowerCase() + ");\n";
        str = str + "\t\t\treturn new ResultMsg(\"修改成功\",true);\n";
        str = str + "\t\t}catch (Exception e){\n\t\t\tlogger.error(e.getMessage());\n\t\t}\n\t\treturn new ResultMsg(\"修改失败\",false);\n\t}";
        functions.add(str);
    }

    public void updateByPrimaryKey() {
        String str = "\t@RequestMapping(value=\"updateByKey\",method = {RequestMethod.POST})\n";
        if (RequiresRoles) {
            str = str + "\t@RequiresRoles(value={\"" + table + ":updateByKey\"}, logical= Logical.OR)";
        }
        if (RequiresPermissions) {
            str = str + "\t@RequiresPermissions(value={\"" + table + ":updateByKey\"}, logical= Logical.OR)";
        }
        str = str + "\t@ApiOperation(value=\"根据id修改" + introspectedTable.getChinese() + "\", notes=\"根据主键修改\")\n" + swaggerString + "\n";
        str = str + "\t@ResponseBody \n";
        str = str + "\tpublic ResultMsg updateByPrimaryKey(HttpServletRequest request) throws Exception {\n";
        str = str + "\t\ttry{\n";
        str = str + "\t\t\t" + tabletype + "  " + tabletype.toLowerCase() + "= JyHttp.getRequestEty(request, " + tabletype + ".class);\n";
//        str = str + "\t\t\t" + ExampleType + "  " + ExampleType.toLowerCase() + " = new " + ExampleType + "();\n";
        str = str + "\t\t\t" + this.Mappers.get(0).substring(this.Mappers.get(0).lastIndexOf(".") + 1).toLowerCase() + ".updateByPrimaryKey(" + tabletype.toLowerCase() + ");\n";
        str = str + "\t\t\treturn new ResultMsg(\"修改成功\",true);\n";
        str = str + "\t\t}catch (Exception e){\n\t\t\tlogger.error(e.getMessage());\n\t\t}\n\t\treturn new ResultMsg(\"修改失败\",false);\n\t}";
        functions.add(str);
    }

    public void selectByPrimaryKey() {
        String str = "\t@RequestMapping(value=\"selectByKey\",method = {RequestMethod.POST})\n";
        if (RequiresRoles) {
            str = str + "\t@RequiresRoles(value={\"" + table + ":selectByKey\"}, logical= Logical.OR)";
        }
        if (RequiresPermissions) {
            str = str + "\t@RequiresPermissions(value={\"" + table + ":selectByKey\"}, logical= Logical.OR)";
        }
        str = str + "\t@ApiOperation(value=\"得到" + introspectedTable.getChinese() + "详情\", notes=\"根据主键获得详情\")\n" + swaggerString + "\n";
        str = str + "\t@ResponseBody\n";
        str = str + "\tpublic  ResultMsg<" + tabletype + "> selectByPrimaryKey(@RequestParam(\"" + keyname + "\") " + keytype + " " + keyname + ") throws Exception {\n";
        str = str + "\t\t" + tabletype + "  " + tabletype.toLowerCase() + " =  " + this.Mappers.get(0).substring(this.Mappers.get(0).lastIndexOf(".") + 1).toLowerCase() + ".selectByPrimaryKey(" + keyname + ");\n" +
                "\t\treturn new ResultMsg(" + tabletype.toLowerCase() + ");\n\t}";
        functions.add(str);
    }

    public void deleteByPrimaryKey() {
        String str = "\t@RequestMapping(value=\"deleteByPrimaryKey\",method = {RequestMethod.POST})\n";
        if (RequiresRoles) {
            str = str + "\t@RequiresRoles(value={\"" + table + ":deleteByPrimaryKey\"}, logical= Logical.OR)";
        }
        if (RequiresPermissions) {
            str = str + "\t@RequiresPermissions(value={\"" + table + ":deleteByPrimaryKey\"}, logical= Logical.OR)";
        }
        str = str + "\t@ApiOperation(value=\"删除" + introspectedTable.getChinese() + "\", notes=\"根据主键删除\")\n" + swaggerString + "\n";
        str = str + "\t@ResponseBody\n";
        str = str + "\tpublic  ResultMsg deleteByPrimaryKey(@RequestParam(\"" + keyname + "\") " + keytype + " " + keyname + ") throws Exception {\n";
        str = str + "\t\ttry {\n" +
                "\t\t\t" + this.Mappers.get(0).substring(this.Mappers.get(0).lastIndexOf(".") + 1).toLowerCase() + ".deleteByPrimaryKey(" + keyname + ");\n" +
                "\t\t\treturn new ResultMsg(\"删除成功\");\n" +
                "\t\t}catch (Exception e){\n\t\t\tlogger.error(e.getMessage());\n\t\t}\n" +
                "\t\treturn new ResultMsg(\"删除失败\",false);\n";
        str = str + "\t}";
        functions.add(str);
    }

}
